#include "SetMEMSDataDialog.h"

#include "MEMSDataHandler.h"
#include "MEMSDataCodec.h"
#include "MEMSDataSaver.h"
#include "SerialPortManager.h"
#include "GroupBox_SetSerialPort.h"

SetMEMSDataDialog::SetMEMSDataDialog(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	setFixedSize(width(),350);
	addWidgetToList();
	slot_updateFilePathLineEdit();
	connect(ui.pushButton_confirm, &QPushButton::clicked, this, &SetMEMSDataDialog::accept);
	connect(ui.pushButton_cancel, &QPushButton::clicked, this, &SetMEMSDataDialog::reject);
	connect(ui.pushButton_choiceFilePath_1, &QPushButton::clicked, this, &SetMEMSDataDialog::slot_openFilePathDialog_1);
	connect(ui.pushButton_choiceFilePath_2, &QPushButton::clicked, this, &SetMEMSDataDialog::slot_openFilePathDialog_2);
	connect(ui.pushButton_choiceFilePath_3, &QPushButton::clicked, this, &SetMEMSDataDialog::slot_openFilePathDialog_3);
	connect(ui.pushButton_choiceFilePath_4, &QPushButton::clicked, this, &SetMEMSDataDialog::slot_openFilePathDialog_4);
	connect(ui.pushButton_choiceFilePath_5, &QPushButton::clicked, this, &SetMEMSDataDialog::slot_openFilePathDialog_5);
	connect(ui.pushButton_choiceFilePath_6, &QPushButton::clicked, this, &SetMEMSDataDialog::slot_openFilePathDialog_6);
	void (QComboBox:: * functionProxy)(int) = &QComboBox::currentIndexChanged;
	connect(ui.comboBox_dataGroupCount, functionProxy, this, &SetMEMSDataDialog::slot_updateFilePathLineEdit);
}

SetMEMSDataDialog::~SetMEMSDataDialog()
{
}
int SetMEMSDataDialog::exec()
{
	//DataCollection
	updateDataCollectionProtocol();
	updateDataGroupCount();
	updateDataFilePath();

	//SerialPort
	ui.groupBox_setSerialPort->showSerialPortInfo(serialPortManager);
	return QDialog::exec();
}

void SetMEMSDataDialog::accept()
{
	//MEMSData
	switch (ui.comboBox_transmissionProtocol->currentIndex())
	{
	case 0:
		mEMSDataHandler->setDataCollectionProtocol(MEMSDataHandler::Protocol_SingleTemperature);
		break;
	case 1:
		mEMSDataHandler->setDataCollectionProtocol(MEMSDataHandler::Protocol_DoubleTemperature);
		break;
	default:
		break;
	}
	switch (ui.comboBox_dataGroupCount->currentIndex())
	{
	case 0:
		mEMSDataCodec->setDataPackagetype(MEMSDataCodec::Single);
		break;
	case 1:
		mEMSDataCodec->setDataPackagetype(MEMSDataCodec::Multi);
		break;
	default:
		break;
	}
	for (int i = 0; i < lineEdit_filePathOfSavings.size(); ++i)
	{
		mEMSDataSaver->setFilePath(i, lineEdit_filePathOfSavings[i]->text());
	}

	//SerialPort
	ui.groupBox_setSerialPort->setUpSerialPort(serialPortManager);
	QDialog::accept();
}

void SetMEMSDataDialog::slot_openFilePathDialog_1()
{
	openFilePathDialog(0);
}
void SetMEMSDataDialog::slot_openFilePathDialog_2()
{
	openFilePathDialog(1);
}
void SetMEMSDataDialog::slot_openFilePathDialog_3()
{
	openFilePathDialog(2);
}
void SetMEMSDataDialog::slot_openFilePathDialog_4()
{
	openFilePathDialog(3);
}
void SetMEMSDataDialog::slot_openFilePathDialog_5()
{
	openFilePathDialog(4);
}
void SetMEMSDataDialog::slot_openFilePathDialog_6()
{
	openFilePathDialog(5);
}
void SetMEMSDataDialog::slot_updateFilePathLineEdit()
{
	switch (ui.comboBox_dataGroupCount->currentIndex())
	{
	case 0:
		for (int i = 1; i < 6; ++i)
		{
			hideFilePathMode(i);
		}
		setFixedSize(width(), 350);
		break;
	case 1:
		for (int i = 1; i < 6; ++i)
		{
			showFilePathMode(i);
		}
		setFixedSize(width(), 650);
		break;
	default:
		break;
	}
}

void SetMEMSDataDialog::updateDataCollectionProtocol()
{
	switch (mEMSDataHandler->getDataCollectionProtocol())
	{
	case MEMSDataHandler::Protocol_SingleTemperature:
		ui.comboBox_transmissionProtocol->setCurrentIndex(0);
		break;
	case MEMSDataHandler::Protocol_DoubleTemperature:
		ui.comboBox_transmissionProtocol->setCurrentIndex(1);
		break;
	default:
		break;
	}
}

void SetMEMSDataDialog::updateDataFilePath()
{
	for (int i = 0; i < mEMSDataSaver->getDataGroupCount(); ++i)
	{
		lineEdit_filePathOfSavings[i]->setText(mEMSDataSaver->getFilePath(i));
	}
}
void SetMEMSDataDialog::updateDataGroupCount()
{
	switch (mEMSDataCodec->getDataPackagetype())
	{
	case MEMSDataCodec::Single:
		ui.comboBox_dataGroupCount->setCurrentIndex(0);
		
		break;
	case MEMSDataCodec::Multi:
		ui.comboBox_dataGroupCount->setCurrentIndex(1);
		
		break;
	default:
		break;
	}
}
void SetMEMSDataDialog::addWidgetToList()
{
	label_filePathOfSavings.append(ui.label_filePathOfSaving_1);
	label_filePathOfSavings.append(ui.label_filePathOfSaving_2);
	label_filePathOfSavings.append(ui.label_filePathOfSaving_3);
	label_filePathOfSavings.append(ui.label_filePathOfSaving_4);
	label_filePathOfSavings.append(ui.label_filePathOfSaving_5);
	label_filePathOfSavings.append(ui.label_filePathOfSaving_6);
	lineEdit_filePathOfSavings.append(ui.lineEdit_filePathOfSaving_1);
	lineEdit_filePathOfSavings.append(ui.lineEdit_filePathOfSaving_2);
	lineEdit_filePathOfSavings.append(ui.lineEdit_filePathOfSaving_3);
	lineEdit_filePathOfSavings.append(ui.lineEdit_filePathOfSaving_4);
	lineEdit_filePathOfSavings.append(ui.lineEdit_filePathOfSaving_5);
	lineEdit_filePathOfSavings.append(ui.lineEdit_filePathOfSaving_6);
	pushButton_filePathOfSavings.append(ui.pushButton_choiceFilePath_1);
	pushButton_filePathOfSavings.append(ui.pushButton_choiceFilePath_2);
	pushButton_filePathOfSavings.append(ui.pushButton_choiceFilePath_3);
	pushButton_filePathOfSavings.append(ui.pushButton_choiceFilePath_4);
	pushButton_filePathOfSavings.append(ui.pushButton_choiceFilePath_5);
	pushButton_filePathOfSavings.append(ui.pushButton_choiceFilePath_6);
}
void SetMEMSDataDialog::openFilePathDialog(int dataGroupIndex)
{
	QFileDialog dialog(this);
	dialog.setAcceptMode(QFileDialog::AcceptSave);
	dialog.setNameFilter(".txt(*.txt)");
	QString filepath;
	if (dialog.exec()) {
		filepath = dialog.selectedFiles().first();
	}
	if (!filepath.isEmpty()) {
		lineEdit_filePathOfSavings[dataGroupIndex]->setText(filepath);
	}
	return;
}
void SetMEMSDataDialog::showFilePathMode(int modeIndex)
{
	label_filePathOfSavings.at(modeIndex)->show();
	lineEdit_filePathOfSavings.at(modeIndex)->show();
	pushButton_filePathOfSavings.at(modeIndex)->show();
}
void SetMEMSDataDialog::hideFilePathMode(int modeIndex)
{
	label_filePathOfSavings.at(modeIndex)->hide();
	lineEdit_filePathOfSavings.at(modeIndex)->hide();
	pushButton_filePathOfSavings.at(modeIndex)->hide();
}
MEMSDataHandler* SetMEMSDataDialog::getMEMSDataHandler()
{
	return mEMSDataHandler;
}
void SetMEMSDataDialog::setMEMSDataHandler(MEMSDataHandler* mEMSDataHandler)
{
	this->mEMSDataHandler = mEMSDataHandler;
}

MEMSDataCodec* SetMEMSDataDialog::getMEMSDataCodec()
{
	return mEMSDataCodec;
}
void SetMEMSDataDialog::setMEMSDataCodec(MEMSDataCodec* mEMSDataCodec)
{
	this->mEMSDataCodec = mEMSDataCodec;
}
MEMSDataSaver* SetMEMSDataDialog::getMEMSDataSaver()
{
	return mEMSDataSaver;
}
void SetMEMSDataDialog::setMEMSDataSaver(MEMSDataSaver* mEMSDataSaver)
{
	this->mEMSDataSaver = mEMSDataSaver;
}
SerialPortManager* SetMEMSDataDialog::getSerialPortManager()
{
	return serialPortManager;
}
void SetMEMSDataDialog::setSerialPortManager(SerialPortManager* serialPortManager)
{
	this->serialPortManager = serialPortManager;
}